package com.community.property.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    // 普通队列指定死信交换机（30秒延迟）
    @Bean
    public Queue repairQueue(){
        return QueueBuilder.durable("repair.queue")
                .deadLetterExchange("repair.dlx.exchange")
                .deadLetterRoutingKey("repair.dlx.key")
                .ttl(30000) // 30秒延迟
                .build();
    }

    @Bean
    public TopicExchange repairExchange() {
        return new TopicExchange("repair.exchange", true, false); // 持久化
    }

    @Bean
    public Binding bindingRepairQueue(@Qualifier("repairQueue") Queue repairQueue,
                                      @Qualifier("repairExchange") TopicExchange repairExchange) {
        return BindingBuilder.bind(repairQueue)
                .to(repairExchange)
                .with("repair.add");
    }

    // 死信队列（用于接收30秒后过期的消息）
    @Bean
    public Queue repairDlxQueue(){
        return new Queue("repair.dlx.queue", true);
    }

    // 死信交换机
    @Bean
    public TopicExchange repairDlxExchange(){
        return new TopicExchange("repair.dlx.exchange", true, false); // 持久化
    }

    // 死信交换机绑定死信队列
    @Bean
    public Binding repairDlxBinding(@Qualifier("repairDlxQueue") Queue repairDlxQueue,
                                    @Qualifier("repairDlxExchange") TopicExchange repairDlxExchange){
        return BindingBuilder.bind(repairDlxQueue)
                .to(repairDlxExchange)
                .with("repair.dlx.key");
    }
}